<?php

class Default_Controller_Admin_Translations extends Light_Controller
{
    public function init()
    {
        if (!$this->view->identity()) {
            $this->view->redirect(array('module' => 'users', 'controller' => 'index', 'action' => 'login'), null, true);
        }

        if (!$this->view->isAdmin()) {
            throw new Light_Exception('Access denied');
        }
    }

	public function indexAction()
	{
	    $errors = array();
	    $selectedTranslations = array();
        $selectedLanguage = $this->getRequest()->getParam('selectedLanguage', LIGHT_LANGUAGE_DEFAULT);

        $files = glob(LIGHT_LANGUAGE_PATH . '/' . $selectedLanguage . '/*');

        if (!empty($files)) {
            asort($files);
            foreach ($files as $file) {
                $parts   = explode('/', $file);
                $section = end($parts);
                $translations = require $file;
                $selectedTranslations[$section] = $translations;
            }
        }

        if ($this->getRequest()->isPost()) {
            $deleteKey = $this->getRequest()->getPost('deleteKey');
            $deleteSection = $this->getRequest()->getPost('deleteSection');
            $isAdd = $this->getRequest()->getPost('add');
            if ($deleteKey && $deleteSection) {
                if (isset($selectedTranslations[$deleteSection][$deleteKey])) {
                    unset($selectedTranslations[$deleteSection][$deleteKey]);
                    $this->_saveTranslations($selectedLanguage, $selectedTranslations);
                }
            } else if ($isAdd) {
                $section = $this->getRequest()->getPost('section');
                $key = $this->getRequest()->getPost('key');
                $value = $this->getRequest()->getPost('value');
                if (!$section || !$key) {
                	$errors['key'] = 'The field is required';
                } else {
	                if (isset($selectedTranslations[$section])) {
	                    if (!isset($selectedTranslations[$section][$key])) {
	                    	$selectedTranslations[$section] = array_merge(array($key => $value), $selectedTranslations[$section]);
	                        $this->_saveTranslations($selectedLanguage, $selectedTranslations);
	                    }
	                }
                }
            } else {
                $translations = $this->getRequest()->getPost('translations', array());
                $this->_saveTranslations($selectedLanguage, $translations);
            }
            if (empty($errors)) {
                $this->view->flashMessage()->setMessage();
                $this->view->redirect(array('selectedLanguage' => $selectedLanguage));
            }
        }

        $this->view->assign('errors', $errors);
        $this->view->assign('selectedTranslations', $selectedTranslations);
        $this->view->assign('selectedLanguage', $selectedLanguage);
	}

	protected function _saveTranslations($selectedLanguage, $data)
	{
	    foreach ($data as $section => $values) {
	        file_put_contents(LIGHT_LANGUAGE_PATH . '/' . $selectedLanguage . '/' . $section, '<?php' . "\n\n" . 'return ' . var_export($values, true) . ';');
	    }
	}
}